package com.example.x6.configurationmaintenance.Module;

import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import com.example.x6.configurationmaintenance.Interface.OnVersionControlGetListener;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class UpdateManager {
    public static final String ApkPath = "ConfigurationMaintenance/update";
    public static final String ServiceURL = "http://47.92.160.161/app/version_control.xml";
    public static final String md5 = "md5";
    public static final String releaseMessage = "releaseMessage";
    public static final String url = "url";
    public static final String version = "version";
    public static final String versionCode = "versionCode";
    private final String TAG = getClass().getSimpleName();
    private OnVersionControlGetListener onVersionControlGetListener;

    public void setOnVersionControlGetListener(OnVersionControlGetListener onVersionControlGetListener2) {
        this.onVersionControlGetListener = onVersionControlGetListener2;
        getVersionMessage(ServiceURL);
    }

    private void getVersionMessage(final String url2) {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.UpdateManager.AnonymousClass1 */

            public void run() {
                try {
                    UpdateManager.this.onVersionControlGetListener.onVersionContolGet(UpdateManager.this.parseXml(UpdateManager.this.getInputStreamFromUrl(url2)));
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                    UpdateManager.this.onVersionControlGetListener.onVersionContolGet(null);
                } catch (IOException e2) {
                    e2.printStackTrace();
                    UpdateManager.this.onVersionControlGetListener.onVersionContolGet(null);
                } catch (SAXException e3) {
                    e3.printStackTrace();
                    UpdateManager.this.onVersionControlGetListener.onVersionContolGet(null);
                }
            }
        }).start();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private InputStream getInputStreamFromUrl(String urlString) throws IOException {
        return ((HttpURLConnection) new URL(urlString).openConnection()).getInputStream();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private HashMap<String, String> parseXml(InputStream inputStream) throws ParserConfigurationException, IOException, SAXException {
        if (inputStream == null) {
            return null;
        }
        HashMap<String, String> hashMap = new HashMap<>();
        NodeList childNodes = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream).getDocumentElement().getChildNodes();
        for (int j = 0; j < childNodes.getLength(); j++) {
            Node childNode = childNodes.item(j);
            if (childNode.getNodeType() == 1) {
                Element childElement = (Element) childNode;
                hashMap.put(childElement.getNodeName(), childElement.getFirstChild().getNodeValue().trim());
                String str = this.TAG;
                Log.w(str, childElement.getNodeName() + "  <-->  \n" + childElement.getFirstChild().getNodeValue().trim());
            }
        }
        return hashMap;
    }

    private void download(String url2, String filePath) {
    }

    public long downLoadApk(Context context, String url2, String message, String apkName) {
        if (url2 == null) {
            return -1;
        }
        Uri uri = Uri.parse(url2);
        DownloadManager manager = (DownloadManager) context.getSystemService("download");
        DownloadManager.Request requestApk = new DownloadManager.Request(uri);
        requestApk.setDestinationInExternalPublicDir(ApkPath, apkName + ".apk");
        requestApk.setVisibleInDownloadsUi(true);
        requestApk.allowScanningByMediaScanner();
        requestApk.setTitle("配置维护更新下载");
        if (message != null && !message.isEmpty()) {
            requestApk.setDescription(message);
        }
        return manager.enqueue(requestApk);
    }
}
